﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class FormAutoDesign : System.Windows.Forms.Form
	{
		public FormAutoDesign(Autodesk.Revit.DB.Document doc)
		{
			this.doc = doc;
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.InitialInformation();
		}

		private void FormAutoDesign_Load(object sender, EventArgs e)
		{
		}

		private void InitialInformation()
		{
			foreach (Element element in YJKElementFilter.GetElementByClass(typeof(PipeType), this.doc))
			{
				this.cboPipeWType.Items.Add(element.Name);
				this.cboPipeFType.Items.Add(element.Name);
			}
			string autoDesignSetting = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.designstyle.ToString());
			string text = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.splitFlow.ToString());
			if (text == "")
			{
				text = "false";
			}
			bool @checked = Convert.ToBoolean(text);
			string autoDesignSetting2 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.closetpan.ToString());
			string autoDesignSetting3 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.urinal.ToString());
			string autoDesignSetting4 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.floordrain.ToString());
			string autoDesignSetting5 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.lavatories.ToString());
			string autoDesignSetting6 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.other.ToString());
			string text2 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.wToxangle.ToString());
			if (text2 == "")
			{
				text2 = "45";
			}
			string text3 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.wSlope.ToString());
			if (text3 == "")
			{
				text3 = "1.2";
			}
			string text4 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.wDiameter.ToString());
			if (text4 == "")
			{
				text4 = "100";
			}
			string text5 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.wOffset.ToString());
			if (text5 == "")
			{
				text5 = "-0.5";
			}
			string text6 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.fToxangle.ToString());
			if (text6 == "")
			{
				text6 = "45";
			}
			string text7 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.fSlope.ToString());
			if (text7 == "")
			{
				text7 = "1.2";
			}
			string text8 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.fDiameter.ToString());
			if (text8 == "")
			{
				text8 = "50";
			}
			string text9 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.fOffset.ToString());
			if (text9 == "")
			{
				text9 = "-0.5";
			}
			string a = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.wChoosePipeType.ToString());
			if (a == "")
			{
				a = "False";
			}
			string a2 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.fChoosePipeType.ToString());
			if (a2 == "")
			{
				a2 = "False";
			}
			string text10 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.wPipeType.ToString());
			if (text10 == "")
			{
				text10 = "8";
			}
			string text11 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.fPipeType.ToString());
			if (text11 == "")
			{
				text11 = "8";
			}
			string a3 = this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.bLinkBathe.ToString());
			if (a3 == "")
			{
				a3 = "False";
			}
			double num = Convert.ToDouble(text2);
			double num2 = Convert.ToDouble(text3);
			double num3 = Convert.ToDouble(text4);
			double num4 = Convert.ToDouble(text5);
			double num5 = Convert.ToDouble(text6);
			double num6 = Convert.ToDouble(text7);
			double num7 = Convert.ToDouble(text8);
			double num8 = Convert.ToDouble(text9);
			if (autoDesignSetting == FormAutoDesign.DesignStyle.transversal.ToString())
			{
				this.groupBox3.Text = "排水管设置";
				this.rdbPipe.Checked = true;
				this.ckbDrawPipe.Enabled = true;
				this.hyTxtWOffset.Enabled = false;
				this.hyTxtFOffset.Enabled = false;
				bool flag = Convert.ToBoolean(this.userSetting.GetAutoDesignSetting(FormAutoDesign.DesignSetting.drawpipe.ToString()));
				this.ckbDrawPipe.Checked = flag;
				this.TextBoxEnabled(flag);
				this.SplitFlowTxtEnabled();
				this.cbxPipeFType.Enabled = false;
				this.cbxPipeWType.Enabled = false;
				this.cboPipeFType.Enabled = false;
				this.cboPipeWType.Enabled = false;
			}
			else
			{
				this.groupBox3.Text = "污水管设置";
				this.rdbRiser.Checked = true;
				this.hyTxtWOffset.Enabled = true;
				this.hyTxtFOffset.Enabled = true;
				this.ckbDrawPipe.Enabled = false;
				this.TextBoxEnabled(true);
				this.SplitFlowTxtEnabled();
			}
			this.ckbSplitFlow.Checked = @checked;
			this.hyTxtWAngle.Text = num.ToString();
			this.hyTxtWSlope.Text = num2.ToString();
			this.hyTxtWOffset.Text = num4.ToString();
			this.hyTxtWDiameter.Text = num3.ToString();
			this.hyTxtFAngle.Text = num5.ToString();
			this.hyTxtFSlope.Text = num6.ToString();
			this.hyTxtFOffset.Text = num8.ToString();
			this.hyTxtFDiameter.Text = num7.ToString();
			this.cmbDa.SelectedItem = autoDesignSetting2;
			this.cmbXiao.SelectedItem = autoDesignSetting3;
			this.cmbDilou.SelectedItem = autoDesignSetting4;
			this.cmbGuan.SelectedItem = autoDesignSetting5;
			this.cmbOther.SelectedItem = autoDesignSetting6;
			int num9 = int.Parse(text11);
			if (this.cboPipeFType.Items.Count > num9)
			{
				this.cboPipeFType.SelectedIndex = num9;
			}
			else if (this.cboPipeFType.Items.Count > 0)
			{
				this.cboPipeFType.SelectedIndex = 0;
			}
			num9 = int.Parse(text10);
			if (this.cboPipeWType.Items.Count > num9)
			{
				this.cboPipeWType.SelectedIndex = num9;
			}
			else if (this.cboPipeWType.Items.Count > 0)
			{
				this.cboPipeWType.SelectedIndex = 0;
			}
			this.cbxPipeFType.Checked = (a2 == "True");
			this.cbxPipeWType.Checked = (a == "True");
			this.cbxLinkBathe.Checked = (a3 == "True");
		}

		private void TextBoxEnabled(bool enabled)
		{
			this.hyTxtWSlope.Enabled = enabled;
			this.hyTxtWAngle.Enabled = enabled;
			this.btnWPick.Enabled = enabled;
			this.hyTxtWDiameter.Enabled = enabled;
			if (enabled && this.cbxPipeWType.Checked)
			{
				this.cboPipeWType.Enabled = true;
			}
			else
			{
				this.cboPipeWType.Enabled = false;
			}
			this.cbxPipeWType.Enabled = enabled;
			this.hyTxtFSlope.Enabled = enabled;
			this.hyTxtFAngle.Enabled = enabled;
			this.btnFPick.Enabled = enabled;
			this.hyTxtFDiameter.Enabled = enabled;
			if (enabled && this.cbxPipeFType.Checked)
			{
				this.cboPipeFType.Enabled = true;
			}
			else
			{
				this.cboPipeFType.Enabled = false;
			}
			this.cbxPipeFType.Enabled = enabled;
			if (this.rdbPipe.Checked)
			{
				this.cbxPipeFType.Enabled = false;
				this.cbxPipeWType.Enabled = false;
				this.cboPipeFType.Enabled = false;
				this.cboPipeWType.Enabled = false;
			}
		}

		public void SetWAngle(double angle)
		{
			this.hyTxtWAngle.Text = angle.ToString();
		}

		public void SetFAngle(double angle)
		{
			this.hyTxtFAngle.Text = angle.ToString();
		}

		public bool GetBeDrawPipe()
		{
			return this.ckbDrawPipe.Checked;
		}

		public double GetWAngle()
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtWAngle.Text);
			}
			catch (Exception)
			{
				result = 0.0;
				throw;
			}
			return result;
		}

		public double GetFAngle()
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtFAngle.Text);
			}
			catch (Exception)
			{
				result = 0.0;
				throw;
			}
			return result;
		}

		public bool GetDesignStyle()
		{
			return this.rdbRiser.Checked;
		}

		public bool GetSplitFlow()
		{
			return this.ckbSplitFlow.Checked;
		}

		public bool IsSeleLinkBath()
		{
			return this.cbxLinkBathe.Checked;
		}

		public double GetWPipeDiameter()
		{
			double result = 100.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtWDiameter.Text);
			}
			catch (Exception)
			{
				result = 100.0;
				throw;
			}
			return result;
		}

		public double GetFPipeDiameter()
		{
			double result = 100.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtFDiameter.Text);
			}
			catch (Exception)
			{
				result = 100.0;
				throw;
			}
			return result;
		}

		public double GetWPipeSlope()
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtWSlope.Text);
			}
			catch (Exception)
			{
				result = 0.0;
				throw;
			}
			return result;
		}

		public double GetFPipeSlope()
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtFSlope.Text);
			}
			catch (Exception)
			{
				result = 0.0;
				throw;
			}
			return result;
		}

		public double GetWPipeStartOffset()
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtWOffset.Text);
			}
			catch (Exception)
			{
				result = 0.0;
				throw;
			}
			return result;
		}

		public double GetFPipeStartOffset()
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(this.hyTxtFOffset.Text);
			}
			catch (Exception)
			{
				result = 0.0;
				throw;
			}
			return result;
		}

		public string GetAccessorySetted(SettingAccessory setAccessory)
		{
			string result;
			switch (setAccessory)
			{
			case SettingAccessory.ClosetPan:
				result = this.cmbDa.SelectedItem.ToString();
				break;
			case SettingAccessory.Urinal:
				result = this.cmbXiao.SelectedItem.ToString();
				break;
			case SettingAccessory.FloorDrain:
				result = this.cmbDilou.SelectedItem.ToString();
				break;
			case SettingAccessory.Lavatories:
				result = this.cmbGuan.SelectedItem.ToString();
				break;
			case SettingAccessory.Other:
				result = this.cmbOther.SelectedItem.ToString();
				break;
			default:
				result = "无";
				break;
			}
			return result;
		}

        public Autodesk.Revit.DB.Plumbing.PipeType GetWPipeType()
		{
            Autodesk.Revit.DB.Plumbing.PipeType result = null;
            List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Autodesk.Revit.DB.Plumbing.PipeType), this.doc);
			if (this.cboPipeWType.Enabled)
			{
                result = (elementByClass[this.cboPipeWType.SelectedIndex] as Autodesk.Revit.DB.Plumbing.PipeType);
			}
			return result;
		}

		public PipeType GetFPipeType()
		{
			PipeType result = null;
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(PipeType), this.doc);
			if (this.cboPipeFType.Enabled)
			{
				result = (elementByClass[this.cboPipeFType.SelectedIndex] as PipeType);
			}
			return result;
		}

		private void hyTxtAngle_Validating(object sender, CancelEventArgs e)
		{
			double num = Convert.ToDouble(this.hyTxtWAngle.Text);
			if (num >= 360.0 || num < 0.0)
			{
				YJKMessageBox.Show("输入角度应在0-360度之间。");
				e.Cancel = true;
			}
		}

		private void hyTxtSlope_Validating(object sender, CancelEventArgs e)
		{
			double num = Convert.ToDouble(this.hyTxtWSlope.Text);
			if (num > 10.0 || num < -10.0)
			{
				YJKMessageBox.Show("输入坡度应在-10%-10%之间。");
				e.Cancel = true;
			}
		}

		private void hyTxtDiameter_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int num = Convert.ToInt32(this.hyTxtWDiameter.Text);
				if (num <= 0 || num >= 3000)
				{
					YJKMessageBox.Show("管径范围应为小3000的正整数！");
					e.Cancel = true;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Show("输入值必须为正整数！");
				throw;
			}
		}

		private void rdbPipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdbPipe.Checked)
			{
				this.groupBox3.Text = "排水管设置";
				this.ckbDrawPipe.Enabled = true;
				this.hyTxtWOffset.Enabled = false;
				this.hyTxtFOffset.Enabled = false;
				this.TextBoxEnabled(this.ckbDrawPipe.Checked);
				this.SplitFlowTxtEnabled();
			}
			if (this.rdbRiser.Checked)
			{
				this.groupBox3.Text = "污水管设置";
				this.ckbDrawPipe.Enabled = false;
				this.hyTxtWOffset.Enabled = true;
				this.hyTxtFOffset.Enabled = true;
				this.TextBoxEnabled(true);
				this.SplitFlowTxtEnabled();
			}
		}

		private void FormAutoDesign_FormClosing(object sender, FormClosingEventArgs e)
		{
			string nodeValue = "";
			if (this.rdbPipe.Checked)
			{
				nodeValue = FormAutoDesign.DesignStyle.transversal.ToString();
			}
			if (this.rdbRiser.Checked)
			{
				nodeValue = FormAutoDesign.DesignStyle.riser.ToString();
			}
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.designstyle.ToString(), nodeValue);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.wToxangle.ToString(), this.hyTxtWAngle.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.fToxangle.ToString(), this.hyTxtFAngle.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.drawpipe.ToString(), this.ckbDrawPipe.Checked.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.splitFlow.ToString(), this.ckbSplitFlow.Checked.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.wDiameter.ToString(), this.hyTxtWDiameter.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.wOffset.ToString(), this.hyTxtWOffset.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.wSlope.ToString(), this.hyTxtWSlope.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.fDiameter.ToString(), this.hyTxtFDiameter.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.fOffset.ToString(), this.hyTxtFOffset.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.fSlope.ToString(), this.hyTxtFSlope.Text);
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.closetpan.ToString(), this.cmbDa.SelectedItem.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.urinal.ToString(), this.cmbXiao.SelectedItem.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.floordrain.ToString(), this.cmbDilou.SelectedItem.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.lavatories.ToString(), this.cmbGuan.SelectedItem.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.other.ToString(), this.cmbOther.SelectedItem.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.wChoosePipeType.ToString(), this.cbxPipeWType.Checked.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.fChoosePipeType.ToString(), this.cbxPipeFType.Checked.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.wPipeType.ToString(), this.cboPipeWType.SelectedIndex.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.fPipeType.ToString(), this.cboPipeFType.SelectedIndex.ToString());
			this.userSetting.WriteAutoDesignSetting(FormAutoDesign.DesignSetting.bLinkBathe.ToString(), this.cbxLinkBathe.Checked.ToString());
		}

		private void btnPick_Click(object sender, EventArgs e)
		{
			this.bAngle = true;
			base.DialogResult = DialogResult.Abort;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnDesign_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Yes;
		}

		private void ckbDrawPipe_CheckedChanged(object sender, EventArgs e)
		{
			this.TextBoxEnabled(this.ckbDrawPipe.Checked);
			this.SplitFlowTxtEnabled();
		}

		private void label1_Click(object sender, EventArgs e)
		{
		}

		private void hyTxtAngle_TextChanged(object sender, EventArgs e)
		{
		}

		private void ckbSplitFlow_CheckedChanged(object sender, EventArgs e)
		{
			this.SplitFlowTxtEnabled();
		}

		private void SplitFlowTxtEnabled()
		{
			if (!this.rdbPipe.Checked)
			{
				if (this.ckbSplitFlow.Checked)
				{
					this.groupBox3.Text = "污水管设置";
					this.hyTxtFOffset.Enabled = true;
					this.hyTxtFDiameter.Enabled = true;
					this.hyTxtFSlope.Enabled = true;
					this.hyTxtFAngle.Enabled = true;
					this.btnFPick.Enabled = true;
					this.cbxPipeFType.Enabled = true;
					if (this.cbxPipeFType.Checked && this.cbxPipeFType.Enabled)
					{
						this.cboPipeFType.Enabled = true;
						return;
					}
				}
				else
				{
					this.groupBox3.Text = "排水管设置";
					this.hyTxtFOffset.Enabled = false;
					this.hyTxtFDiameter.Enabled = false;
					this.hyTxtFSlope.Enabled = false;
					this.hyTxtFAngle.Enabled = false;
					this.btnFPick.Enabled = false;
					this.cbxPipeFType.Enabled = false;
					this.cboPipeFType.Enabled = false;
				}
				return;
			}
			this.cbxPipeFType.Enabled = false;
			this.cbxPipeWType.Enabled = false;
			this.cboPipeFType.Enabled = false;
			this.cboPipeWType.Enabled = false;
			if (!this.ckbDrawPipe.Checked)
			{
				return;
			}
			if (this.ckbSplitFlow.Checked)
			{
				this.groupBox3.Text = "污水管设置";
				this.hyTxtFDiameter.Enabled = true;
				this.hyTxtFSlope.Enabled = true;
				this.hyTxtFAngle.Enabled = true;
				this.btnFPick.Enabled = true;
				return;
			}
			this.groupBox3.Text = "排水管设置";
			this.hyTxtFDiameter.Enabled = false;
			this.hyTxtFSlope.Enabled = false;
			this.hyTxtFAngle.Enabled = false;
			this.btnFPick.Enabled = false;
		}

		private void hyTxtFAngle_Validating(object sender, CancelEventArgs e)
		{
			double num = Convert.ToDouble(this.hyTxtFAngle.Text);
			if (num >= 360.0 || num < 0.0)
			{
				YJKMessageBox.Show("输入角度应在0-360度之间。");
				e.Cancel = true;
			}
		}

		private void hyTxtFSlope_Validating(object sender, CancelEventArgs e)
		{
			double num = Convert.ToDouble(this.hyTxtFSlope.Text);
			if (num > 10.0 || num < -10.0)
			{
				YJKMessageBox.Show("输入坡度应在-10%-10%之间。");
				e.Cancel = true;
			}
		}

		private void btnFPick_Click(object sender, EventArgs e)
		{
			this.bAngle = false;
			base.DialogResult = DialogResult.Abort;
		}

		private void cbxPipeWType_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbxPipeWType.Enabled)
			{
				this.cboPipeWType.Enabled = this.cbxPipeWType.Checked;
			}
		}

		private void cbxPipeFType_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbxPipeFType.Enabled)
			{
				this.cboPipeFType.Enabled = this.cbxPipeFType.Checked;
			}
		}

		private Document doc;

		private HVACUserSetting userSetting = new HVACUserSetting();

		public bool bAngle = true;

		private enum DesignSetting
		{
			designstyle,
			splitFlow,
			drawpipe,
			wToxangle,
			wDiameter,
			wOffset,
			wSlope,
			fToxangle,
			fDiameter,
			fOffset,
			fSlope,
			closetpan,
			urinal,
			floordrain,
			lavatories,
			other,
			wChoosePipeType,
			fChoosePipeType,
			wPipeType,
			fPipeType,
			bLinkBathe
		}

		private enum DesignStyle
		{
			transversal,
			riser
		}

		private enum AccessorySetting
		{
			P弯,
			S弯,
			无
		}
	}
}
